package com.telecom.restaurant.controllor.user;

import com.telecom.restaurant.constant.DataResultConstant;
import com.telecom.restaurant.domin.DataResult;
import com.telecom.restaurant.exception.BizException;
import com.telecom.restaurant.model.CustomerPO;
import com.telecom.restaurant.service.AdminService;
import com.telecom.restaurant.service.CustomerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserLoginController {


    @Autowired
    private CustomerService customerService;

    @Autowired
    private AdminService adminService;


    @RequestMapping("/callbackFromWX")
    @ResponseBody
    public DataResult callbackFromWX(String code) {

        if (StringUtils.isBlank(code)) {
            return DataResultConstant.WRONG_PARAM;
        }

        try {
            CustomerPO customerPO = customerService.getOrCreateCustomer(code);
            // 生成token
            String newToken = adminService.generateUseToken(customerPO.getId(), customerPO.getMobile());
            DataResult dataResult = DataResultConstant.buildSuccessResult(customerPO);
            dataResult.setToken(newToken);
            return dataResult;
        } catch (BizException e) {

            return e.getDataResult();
        }
    }
}
